<template>
    <div class="app-container">
        <div class="warp_padding">  
            <div class="head-container">
                <!-- 搜索 -->
                <label id="labelQuery" class="labelQuery">教室名称: </label>
                <el-input v-model="formDate.roomName" clearable placeholder="教室名称搜索" style="width: 140px;" class="filter-item" @keyup.enter.native="toQuery" @change="toQuery()" />
                

                <span v-if="device!=='mobile'">
                    <label class="labelQuery">设备号: </label>
                    <el-input v-model="formDate.deviceNumber" clearable placeholder="设备号搜索" style="width: 140px;" class="filter-item" @keyup.enter.native="toQuery" @change="toQuery()" />

                    <label class="labelQuery">学校: </label>
                    <el-input v-model="formDate.schName" clearable placeholder="输入学校搜索" style="width: 140px;" class="filter-item" @keyup.enter.native="toQuery" @change="toQuery()" />
                     
                    <label class="labelQuery">教室类型: </label>
                    <el-select v-model="formDate.schType"  clearable placeholder="教室类型搜索" @change="toQuery()" style="width: 140px;">
                        <el-option
                              v-for="item in roomTypes"
                              :key="item.id"
                              :label="item.name"
                              :value="item.id" style="margin-top:5px">
                        </el-option>
                    </el-select>

                    <!-- <label class="labelQuery">校群: </label>
                    <el-select v-model="formDate.schLevel" clearable placeholder="请选择校群搜索" style="width: 150px;" @change="toQuery()">
                        <el-option
                              v-for="item in roomTypes"
                              :key="item.id"
                              :label="item.name"
                              :value="item.id" style="margin-top:5px">
                        </el-option>
                    </el-select> -->

                    <label class="labelQuery">县/区: </label>
                    <area-item @getMessage="showMsg"  style="height:5px!important"/>
                </span>

                <el-button class="filter-item" size="mini" type="primary" icon="el-icon-search" @click="toQuery">搜索</el-button>
                <!-- 新增 -->
                <div style="display: inline-block;margin: 0px 2px;">
                  <el-button
                    v-if="checkPermission(['areclass_all','areclass_add'])"
                    class="filter-item"
                    size="mini"
                    type="primary"
                    icon="el-icon-plus"
                    @click="add">新增</el-button>
                  <eForm ref="form" :is-add="isAdd" :formworks="formworks" :schIds="schIds" :roomTypes="roomTypes"/>
                </div>
            </div>

            <!--列表-->
            <el-table :data="lists" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;" max-height="650">
                <el-table-column type="selection" width="30"></el-table-column>
                
                <el-table-column type="index" width="45"></el-table-column>

                <el-table-column prop="roomName" label="教室名称" :show-overflow-tooltip="true" ></el-table-column>
                
                <el-table-column prop="areaName" label="所属县/区"  :show-overflow-tooltip="true" ></el-table-column>           
                <!-- <el-table-column prop="schName"  :show-overflow-tooltip="true" label="校群名称" width="180"></el-table-column> -->

                <el-table-column prop="schName"  :show-overflow-tooltip="true" label="学校名称" ></el-table-column>
                 
                
                <!-- <el-table-column prop="schLevel" label="班级名称" width="120"></el-table-column>   -->

                <el-table-column prop="deviceNumber" label="设备号"  :show-overflow-tooltip="true"></el-table-column>


                <el-table-column prop="status" label="状态"   sortable>
                     <template slot-scope="scope">
                        <el-tag :type="scope.row.status ? 'info' : 'success'">{{ scope.row.status ? '禁用' : '启用' }}</el-tag>
                    </template>
                </el-table-column>           

                <el-table-column prop="roomType" label="教室类型" :show-overflow-tooltip="true" :formatter="roomType"></el-table-column>            

                <el-table-column prop="address" label="班级地址" :show-overflow-tooltip="true" ></el-table-column>
                
                

                <!-- <el-table-column prop="createtime" label="创建日期" :show-overflow-tooltip="true" sortable>
                    <template slot-scope="scope">
                      <span>{{ parseTime(scope.row.createtime) }}</span>
                    </template>
                </el-table-column> -->

                <el-table-column label="操作" width="430" >
                    <template slot-scope="scope">
                    	<el-button size="mini" type="success" v-if="checkPermission(['areclass_all','areclass_edit'])" @click="handleRole(scope.$index, scope.row.id)">一键授权</el-button>
                    	<el-button size="mini" type="warning" v-if="checkPermission(['areclass_all','areclass_edit']) && scope.row.runningState =='0'" @click="handleFlow(scope.row.id, scope.row.runningState)">关闭推流</el-button>
                        <el-button size="mini" type="warning" v-if="checkPermission(['areclass_all','areclass_edit']) && scope.row.runningState =='1'" @click="handleFlow(scope.row.id, scope.row.runningState)">开启推流</el-button>
                        <el-button size="mini" type="success" v-if="checkPermission(['areclass_all','areclass_edit'])" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button size="mini"  type="primary" v-if="checkPermission(['areclass_all','areclass_edit']) && scope.row.liveStatus =='0'" @click="handleLiveStatus(scope.row.id, scope.row.liveStatus)">关闭直播</el-button>
                        <el-button size="mini"  type="primary" v-if="checkPermission(['areclass_all','areclass_edit']) && scope.row.liveStatus =='1'" @click="handleLiveStatus(scope.row.id, scope.row.liveStatus)">开通直播</el-button>
                        <el-button type="danger" size="mini" v-if="checkPermission(['areclass_all','areclass_del'])" @click="handleDel(scope.$index, scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" size="mini" @click="batchRemove" :disabled="this.sels.length===0" v-if="checkPermission(['sch_all','sch_del'])" class="remove">批量删除</el-button>

            <el-pagination background  @size-change="handleSizeChange"  @current-change="handleCurrentChange"  :current-page.sync="formDate.pageNo" :page-count="pageCount" :page-sizes="[10, 20, 30, 50,100]"  layout="total, sizes,prev, pager, next, jumper" :pager-count="pagerCount" :total="total" class="pagination"></el-pagination>
        </el-col>
    </div>
</template>

<script>

import { mapGetters } from 'vuex'
import areaItem from '@/components/area/areaitem'
import checkPermission from '@/utils/permission' 
import { parseTime } from '@/utils/index'
import eForm from './module/form'
import { areaClassroomList, classroomDel, formwork, allSchoolList, classLiveStatus, classFlow, } from '@/api/request'
import { oneClick } from '@/api/data'
import { debounce } from '@/utils/validate'
export default {
    components: { eForm, areaItem},
    computed: {
        ...mapGetters([
          'device'
        ])
    },
    data() {
        return {
            formworks:[],
            schIds:[],
            pageCount: 0,
            total:0,  
            listLoading: false,
            lists: [],
            sels: [],//列表选中列
            pagerCount:11,   
            formDate:{
                pageNo: 1,  
                pageSize:10,
                roomName:'',
                deviceNumber:'',
                schName:'',
                schType:'',  
                areaCode:'',
                
            },
            isAdd:true,
            roomTypes: [
                {
                    id: 0,
                    name: '精品教室'
                },
                {
                    id: 1,
                    name: '主讲教室'
                },
                {
                    id: 2,
                    name: '专业互动教室'
                },
                {
                    id: 3,
                    name: '直播互动教室'
                },
                {
                    id: 4,
                    name: '直播教室'
                },
            ],
         }
    },
    methods: {
        parseTime,
        checkPermission,
        showMsg(tir){
            this.formDate.areaCode = tir;
            this.init()
        },
        roomType(row, column) {
            return row.roomType == 0 ? '精品教室' : row.roomType == 1 ? '主讲教室' : row.roomType == 2 ? '专业互动教室' : row.roomType == 3 ? '直播互动教室' : row.roomType == 4 ? '直播教室': '未知';
        },
        selsChange: function (sels) {
                this.sels = sels;
        },
        handleCurrentChange(val) {
            this.formDate.pageNo = val;
            this.init()
        },
        handleSizeChange(val) {
            this.formDate.pageSize = val
            this.init()
            // console.log(`page ${val}`);
        },
        toQuery(){
            this.init()
        },
        add(){
            this.isAdd = true
            this.$refs.form.form = { 
                id:'',
                roomName:'',        //教室名称
                deviceNumber: '',   //设备号
                roomType:'',        //教室类型
                schId:'',          //所属学校(区域创建才有)           
                crModuleId:'',     //互动模板
                roommemberCount:'',  //房间访问人数上限
                status: 0,
                roomInfo:'',      //备注
                address:'',
            },
            this.allSch()
            this.ready()
            this.$refs.form.dialog = true
        },
        init(){
            this.listLoading = true
            let params = {...this.formDate}
            areaClassroomList(params).then(res => { 
                let { list, total } = res
                this.lists = list
                this.total = total
                this.listLoading = false 
            })
        },
        handleRole(index,id){
            this.listLoading = true
            oneClick(id).then(res => {
                if (res.code == 2000) {
                    setTimeout(()=> {
                        this.listLoading = false;
                        this.$message({
                            message: '操作成功',
                            duration:1000,
                            type: 'success'
                        });
                        this.init()
                    }, 1000 );
                }else{
                    this.$message({
                        message: res.msg,
                        type: 'error'
                    });
                }
                
            })
        },
        handleFlow:debounce(function(id,runningState){
            let params = {
                id:id,
                runningState:runningState
            }
            classFlow(params).then(res => {
                this.listLoading = false;
                this.$message({
                    message: '操作成功',
                    duration:1000,
                    type: 'success'
                });
                this.init();
            })
        },500),
        handleEdit(index, row) {
            this.isAdd = false
            this.$refs.form.dialog = true
            // let Resuts = Object.assign({}, row) 
            // let isCock = Resuts.isCock.toString()
            // let schLevel  = Resuts.schLevel.toString()
            // Resuts.isCock = isCock
            // Resuts.schLevel = schLevel
            this.allSch()
            this.ready()
            this.$refs.form.form = Object.assign({}, row) 
        },
        handleLiveStatus:debounce(function(id,liveStatus){
            let params = {
                id:id,
                liveStatus:liveStatus
            }
            classLiveStatus(params).then(res => {
                this.listLoading = false;
                this.$message({
                    message: '操作成功',
                    duration:800,
                    type: 'success'
                });
                this.init();
            })
        },500),
        batchRemove(){
            let ids = this.sels.map(item => item.id);
            this.$confirm('确认删除选中的记录吗?', '提示', {
                        type: 'warning'
                    }).then(() => {
                        this.listLoading = true;
                        classroomDel(ids).then(res => {
                            this.listLoading = false;
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            if (this.lists.length == 1 && this.formDate.pageNo != 1) {
                                this.formDate.pageNo = this.formDate.pageNo -1
                            }
                            this.init();
                        })

                    }).catch(() => {
            });
        },
        handleDel(index,ID){
            let ids = [];
            ids.unshift(ID)
            this.$confirm('确认删除该记录吗?', '提示', {
                        type: 'warning'
                    }).then(() => {
                        // console.log(ids)
                        this.listLoading = true;
                        classroomDel(ids).then(res => {
                            this.listLoading = false;
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            if (this.lists.length == 1 && this.formDate.pageNo != 1) {
                                this.formDate.pageNo = this.formDate.pageNo -1
                            }
                            this.init();
                        })

                    }).catch(() => {
            });
        },
        async ready(){
            try {
                let data = await formwork()
                this.formworks = data.data
            } catch (e) {
                console.log(e)
            }

        },
        async allSch(){
            try {
                let data = await allSchoolList()
                this.schIds = data.data
            } catch (e) {
                console.log(e)
            }

        },
        
    },
    mounted:function(){
        this.init()
    },
}

</script>


<style scoped lang="scss">
.el-button--mini{
    padding: 6px 9px!important;
}
</style>
